package com.isnotlocus.gulimail.product.service.impl;


import com.isnotlocus.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.isnotlocus.gulimail.product.entity.AttrEntity;
import com.isnotlocus.gulimail.product.service.AttrAttrgroupRelationService;
import com.isnotlocus.gulimail.product.service.AttrService;
import com.isnotlocus.gulimail.product.vo.AttrGroupAndAttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.Query;

import com.isnotlocus.gulimail.product.dao.AttrGroupDao;
import com.isnotlocus.gulimail.product.entity.AttrGroupEntity;
import com.isnotlocus.gulimail.product.service.AttrGroupService;


@Service()
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    private PageUtils pageUtils;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationEntity;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //page（）这个是ServiceImpl里面的一个属性,里面传入第一个分页信息的对象，
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        //page（）这个是ServiceImpl里面的一个属性,里面传入第一个分页信息的对象，
        //获取一个IPage对象，这个是mybatis-plus的东西
        String key = (String) params.get("key");
        System.out.println(key);
        //只是最终sql语句，默认就只搜寻id的关键字和name的模糊匹配
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like key)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        /**
         * 空wrapper 代表就是没有传入筛选条件，就是默认查询全部
         */
        if (StringUtils.isNotEmpty(key)) {
            System.out.println("==========" + key);
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            //用自己写的工具类构建分页信息
            return new PageUtils(page);
        } else {
            //有携带catId
            //只是最终sql语句，默认就只搜寻id的关键字和name的模糊匹配
            //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like key)
            wrapper.eq("catelog_id", catId);
            //当搜索条件不为空
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupAndAttrVo> SelectAttrGroupMsgAndAttrMsg(Long CateGoryId) {
        List<AttrGroupEntity> list = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", CateGoryId));
        System.out.println("===>>>>>" + list);
        List<AttrGroupAndAttrVo> AttrGroupAndAttrVos = null;
        if (list != null && list.size() > 0) {
            AttrGroupAndAttrVos = list.stream().map((item) -> {
                AttrGroupAndAttrVo attrGroupAndAttrVo = new AttrGroupAndAttrVo();
                List<AttrEntity> AttrEntitys = attrService.getAttrByAttrgourp(item.getAttrGroupId());
                System.out.println("AttrEntitys===" + AttrEntitys);
                if (AttrEntitys != null && AttrEntitys.size() > 0) {
                    BeanUtils.copyProperties(item, attrGroupAndAttrVo);
                    attrGroupAndAttrVo.setAttrs(AttrEntitys);
                }
                return attrGroupAndAttrVo;
            }).filter((item) -> {
                if(item.getAttrGroupId()!=null){
                    return true;
                }
                return false;


            }).collect(Collectors.toList());
        }
        return AttrGroupAndAttrVos;
    }

}